package master2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenyw
 * @date 2022/9/24 19:16
 * 二叉树的所有路径
 */
public class M257binaryTreePaths {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root, new StringBuffer());
        return res;
    }

    private void dfs(TreeNode root, StringBuffer sb) {
        if (root == null) {
            return;
        }

        if (root.left == null && root.right == null) {
            res.add(sb.toString() + root.val);
            return;
        }
        int length = sb.length();
        sb.append(root.val);
        sb.append("->");
        dfs(root.left, sb);
        dfs(root.right, sb);
        sb.delete(length, sb.length());
    }
}
